/*
 * Copyright (c) 2012 Universidad Antonio de Nebrija.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Apache License v2.0
 * which accompanies this distribution, and is available at
 * http://www.apache.org/licenses/LICENSE-2.0.html
 * 
 * Contributors:
 *    Universidad Antonio de Nebrija - initial API, implementation and documentation
 */
package com.nebrija.riif.model;

/**
 * Abstract class with default behaviour to be inherited by other classes.
 * Implements a function that may be needed in the calculation of values
 * @author asanchep
 *
 */
public abstract class Function implements FunctionInterface{

	/** the function name */
	private String name;
	/** the number of parameters */
	private int numOp;
	
	/**
	 * Creates a function
	 * @param name the function name
	 * @param numOp the number of parameters
	 */
	public Function(String name, int numOp){
		this.setName(name);
		this.numOp=numOp;
	}
	
	@Override
	public Object processOperands(Object... operands) {
		// Check the number of operands and returns null if they are not enough
		if(numOp!=-1 && operands.length!=numOp){
			return null;
		}
		return executeFunction(operands);
	}

	/**
	 * Function to be implemented by derived classes.
	 * It calculates a value as a result of a set of operands
	 * @param operands parameters sent to the function
	 * @return the resulting value
	 */
	protected abstract Object executeFunction(Object... operands);
	
	@Override
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	
}
